package com.congee02;

public class MaxAreaOfIsland {

    public int maxAreaOfIsland(int[][] grid) {

        int m = grid.length;
        int n = grid[0].length;

        int ans = 0;

        for (int i = 0 ; i < m ; i ++ ) {
            for (int j = 0 ; j < n ; j ++ ) {
                ans = Math.max(ans, dfsHelp(grid, m, n, i, j));
            }
        }

        return ans;
    }

    private int dfsHelp(int[][] grid, int m, int n, int x, int y) {
        if (x >= m || x < 0 || y >= n || y < 0 || grid[x][y] != 1) {
            return 0;
        }

        grid[x][y] = -1;

        int ans = 1;
        ans += dfsHelp(grid, m, n, x - 1, y);
        ans += dfsHelp(grid, m, n, x + 1, y);
        ans += dfsHelp(grid, m, n, x, y + 1);
        ans += dfsHelp(grid, m, n, x, y - 1);

        return ans;
    }

}
